package org.footballmanager.pandafootball.system.controller.hy;

import org.footballmanager.pandafootball.common.constants.Constants;
import org.footballmanager.pandafootball.system.controller.BaseController;
import org.footballmanager.pandafootball.system.form.impl.FootRankSearchForm;
import org.footballmanager.pandafootball.system.service.FootQdService;
import org.footballmanager.pandafootball.system.service.FootRankService;
import org.footballmanager.pandafootball.system.service.FootSjService;
import org.footballmanager.pandafootball.system.service.FootUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/hy/footRank")
public class HyRankController extends BaseController {
    /* 实体名称 */
    private static final String ENTITYNAME = "footRank";
    private static final String LISTPAGE = String.format(Constants.LISTPAGE, Constants.HY, ENTITYNAME);
    private static final String VIEWPAGE = String.format(Constants.VIEWPAGE, Constants.HY, ENTITYNAME);

    @Autowired
    private FootRankService footRankService;

    @Autowired
    private FootSjService footSjService;

    @Autowired
    private FootUserService footUserService;

    @Autowired
    private FootQdService footQdService;

    @ModelAttribute
    public void init(Model map, HttpServletRequest request, @ModelAttribute FootRankSearchForm footRankSearchForm) {
        map.addAttribute("userList", footUserService.getAllWithoutAdmin(request));
        map.addAttribute("sjList", footSjService.getall());
        map.addAttribute("qdList", footQdService.getall());
        FootRankSearchForm newFootRankSearchForm = new FootRankSearchForm();
        if (footRankSearchForm != null) {
            newFootRankSearchForm = footRankSearchForm;
        }
        map.addAttribute(String.format(Constants.FORMNAME, ENTITYNAME), newFootRankSearchForm);
    }

    @RequestMapping(value = "/search")
    public String search(Model map, @ModelAttribute FootRankSearchForm footRankSearchForm) {
        footRankSearchForm.setSj(footSjService.getSj());
        map.addAttribute(String.format(Constants.FORMNAME, ENTITYNAME), footRankSearchForm);
        map.addAttribute(String.format(Constants.LISTENTITY, ENTITYNAME), footRankService.findall(footRankSearchForm));
        return LISTPAGE;
    }


    @RequestMapping(value = "/view", method = RequestMethod.GET)
    public String openViewPage(Model map, @RequestParam Long id, @ModelAttribute FootRankSearchForm footRankSearchForm) {
        map.addAttribute(String.format(Constants.FORMNAME, ENTITYNAME), footRankSearchForm);
        map.addAttribute(ENTITYNAME, footRankService.get(id));
        return VIEWPAGE;
    }

}
